package com.orangelife.mobile.login.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class SelectCommunityListViewAdapter<T> extends FatherAdapter<T> {

    private Context mContext;
    private LayoutInflater listContainer;
    private List<Map<String, Object>> item_name = null;

    public final class ListItemView {
        public TextView tv_item_name;
        public TextView tv_distance;
        public TextView tv_address;
    }

    public SelectCommunityListViewAdapter(Context context,
                                          List<Map<String, Object>> item_name) {
        this.mContext = context;
        listContainer = LayoutInflater.from(mContext);
        this.item_name = item_name;
    }

    @Override
    public int getCount() {
        return item_name.size();
    }

    @Override
    public Object getItem(int position) {
        return item_name.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ListItemView listItemView;

        if (convertView == null) {
            listItemView = new ListItemView();
            convertView = listContainer.inflate(
                    R.layout.item_lv_select_community, null);

            listItemView.tv_item_name = (TextView) convertView
                    .findViewById(R.id.tv_community_name);
            listItemView.tv_distance = (TextView) convertView
                    .findViewById(R.id.tv_community_distance);
            listItemView.tv_address = (TextView) convertView.findViewById(R.id.tv_community_address);
            listItemView.tv_address.setVisibility(View.VISIBLE);
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }
        listItemView.tv_item_name.setText(String.valueOf(item_name.get(position).get("comm")));
        listItemView.tv_address.setText(String.valueOf(item_name.get(position).get("stree")));
        return convertView;
    }


    @Override
    public BaseAdapter setList(List<T> list) {
        this.item_name = (List<Map<String, Object>>) list;
        return SelectCommunityListViewAdapter.this;
    }

}